package com.mmall.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * Created by root on 18-4-8.
 */
public class FtpUtil {

    private final static String ftpIp = PropertyUtil.getProperty("ftp.server.ip");
    private final static String ftpUser = PropertyUtil.getProperty("ftp.user");
    private final static String ftpPassword = PropertyUtil.getProperty("ftp.password");
    private final static int ftpPort = Integer.parseInt(PropertyUtil.getProperty("ftp.server.port"));

    private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);

    public static boolean upload(String romotePath, File file){
        boolean isSuccess = false;
        FTPClient ftpClient  = new FTPClient();
        if(connect(ftpClient, romotePath)){
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                ftpClient.storeFile(file.getName(), fis);
                isSuccess = true;
            } catch (FileNotFoundException e) {
                logger.error("read file into stream failed .",e);
            } catch (IOException e) {
                logger.error("save file failed .",e);
            } finally {
                close(ftpClient, fis);
            }
        }
        return isSuccess;
    }

    public static boolean upload(String romotePath, List<File> fileList) {
        if (fileList == null || fileList.size() == 0) {
            return false;
        }
        boolean isSuccess = false;
        FTPClient ftpClient = new FTPClient();
        if (connect(ftpClient, romotePath)) {
            FileInputStream fis = null;
            try {
                for (File file : fileList) {
                    fis = new FileInputStream(file);
                    ftpClient.storeFile(file.getName(), fis);
                }
                isSuccess = true;
            } catch (FileNotFoundException e) {
                logger.error("read file into stream failed .", e);
            } catch (IOException e) {
                logger.error("save file failed .", e);
            } finally {
                close(ftpClient, fis);
            }
        }
        return isSuccess;

    }

    private static void close(FTPClient ftpClient, FileInputStream fis) {
        try {
            if (fis != null) {
                fis.close();
            }
            if (ftpClient != null) {
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            logger.error("disconnect ftp client failed .", e);
        }
    }


    private static boolean connect(FTPClient ftpClient, String romotePath){
        boolean isSuccess = false;
        if (ftpClient == null) {
            return isSuccess;
        }
        try {
            ftpClient.connect(ftpIp,ftpPort);
            isSuccess = ftpClient.login(ftpUser, ftpPassword);
            if (isSuccess) {
                ftpClient.setBufferSize(1024);
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.enterLocalPassiveMode();
                if (!StringUtils.isBlank(romotePath)) {
                    ftpClient.changeWorkingDirectory(romotePath);
                }
            }
        } catch (IOException e) {
            logger.error("connect to ftp failed .",e);
        }
        return isSuccess;
    }



}
